<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="费用类型" prop="projectCostType">
        <el-select v-model="queryParams.projectCostType" placeholder="请选择费用类型" clearable>
          <el-option
            v-for="dict in dict.type.project_project_cost_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="费用类别" prop="projectCostCategory">
        <el-select v-model="queryParams.projectCostCategory" placeholder="请选择费用类别" clearable>
          <el-option
            v-for="dict in dict.type.project_project_cost_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="费用出处" prop="projectCostFromUser">
        <el-input
          v-model="queryParams.projectCostFromUser"
          placeholder="请输入谁支出/从哪收入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用时间">
        <el-date-picker
          v-model="daterangeProjectCostDate"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="费用金额" prop="projectCostAmount">
        <el-input
          v-model="queryParams.projectCostAmount"
          placeholder="请输入费用金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用到处" prop="projectCostToUser">
        <el-input
          v-model="queryParams.projectCostToUser"
          placeholder="请输入支出给谁/收入到哪"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:projectCost:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:projectCost:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:projectCost:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:projectCost:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectCostList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目费用id" align="center" prop="projectCostId" v-if="true"/>
      <el-table-column label="费用类型" align="center" prop="projectCostType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_project_cost_type" :value="scope.row.projectCostType"/>
        </template>
      </el-table-column>
      <el-table-column label="费用类别" align="center" prop="projectCostCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_project_cost_category" :value="scope.row.projectCostCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="费用出处" align="center" prop="projectCostFromUser" />
      <el-table-column label="费用时间" align="center" prop="projectCostDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.projectCostDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="费用金额" align="center" prop="projectCostAmount" />
      <el-table-column label="费用到处" align="center" prop="projectCostToUser" />
      <el-table-column label="费用发生的详情" align="center" prop="projectCostContent" />
      <el-table-column label="关联的项目id" align="center" prop="projectId" />
      <el-table-column label="关联的工单id" align="center" prop="workOrderId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:projectCost:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:projectCost:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目费用对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="费用类型" prop="projectCostType">
          <el-select v-model="form.projectCostType" placeholder="请选择费用类型">
            <el-option
              v-for="dict in dict.type.project_project_cost_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="费用类别" prop="projectCostCategory">
          <el-select v-model="form.projectCostCategory" placeholder="请选择费用类别">
            <el-option
              v-for="dict in dict.type.project_project_cost_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="费用出处" prop="projectCostFromUser">
          <el-input v-model="form.projectCostFromUser" placeholder="请输入谁支出/从哪收入" />
        </el-form-item>
        <el-form-item label="费用时间" prop="projectCostDate">
          <el-date-picker clearable
            v-model="form.projectCostDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择费用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="费用金额" prop="projectCostAmount">
          <el-input v-model="form.projectCostAmount" placeholder="请输入费用金额" />
        </el-form-item>
        <el-form-item label="费用到处" prop="projectCostToUser">
          <el-input v-model="form.projectCostToUser" placeholder="请输入支出给谁/收入到哪" />
        </el-form-item>
        <el-form-item label="费用描述" prop="projectCostContent">
          <el-input v-model="form.projectCostContent" type="textarea" placeholder="请输入费用发送的详情" />
        </el-form-item>
        <el-form-item label="项目ID" prop="projectId">
          <el-select v-model="form.projectId" filterable placeholder="请选择项目ID">
            <el-option
              v-for="project in projectList"
              :key="project.projectId"
              :label="project.projectId + '#'+project.projectNumber"
              :value="project.projectId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工单号" prop="workOrderId">
          <el-select v-model="form.workOrderId" filterable placeholder="请选择工单号">
            <el-option
              v-for="item in workOrderList"
              :key="item.workOrderId"
              :label="item.workOrderId"
              :value="item.workOrderId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProjectCost, getProjectCost, delProjectCost, addProjectCost, updateProjectCost } from "@/api/system/projectCost";
import { listProject } from "@/api/system/project";
import { listWorkOrder} from "@/api/system/workOrder";


export default {
  name: "ProjectCost",
  dicts: ['project_project_cost_type', 'project_project_cost_category'],
  data() {
    return {
            //项目列表
            projectList: [],
              //工单列表
              workOrderList: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目费用表格数据
      projectCostList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeProjectCostDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectCostType: undefined,
        projectCostCategory: undefined,
        projectCostFromUser: undefined,
        projectCostDate: undefined,
        projectCostAmount: undefined,
        projectCostToUser: undefined,
        projectId: undefined,
        workOrderId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectCostId: [
          { required: true, message: "项目费用id不能为空", trigger: "blur" }
        ],
        projectCostType: [
          { required: true, message: "费用类型不能为空", trigger: "change" }
        ],
        projectCostCategory: [
          { required: true, message: "费用类别不能为空", trigger: "change" }
        ],
        projectCostFromUser: [
          { required: true, message: "谁支出/从哪收入不能为空", trigger: "blur" }
        ],
        projectCostDate: [
          { required: true, message: "费用时间不能为空", trigger: "blur" }
        ],
        projectCostAmount: [
          { required: true, message: "费用金额不能为空", trigger: "blur" }
        ],
        projectCostToUser: [
          { required: true, message: "支出给谁/收入到哪不能为空", trigger: "blur" }
        ],
        projectCostContent: [
          { required: true, message: "费用发生的详情不能为空", trigger: "blur" }
        ],
        projectId: [
          { required: true, message: "关联的项目id不能为空", trigger: "change" }
        ],
        workOrderId: [
          { required: true, message: "关联的工单id不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
      //查询项目列表
      listProject().then(response => {
        this.projectList = response.rows;
    });
    //查询工单列表
    listWorkOrder().then(response => {
      this.workOrderList = response.rows;
    })
    this.getList();
  },
  methods: {
    /** 查询项目费用列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeProjectCostDate && '' != this.daterangeProjectCostDate) {
        this.queryParams.params["beginProjectCostDate"] = this.daterangeProjectCostDate[0];
        this.queryParams.params["endProjectCostDate"] = this.daterangeProjectCostDate[1];
      }
      listProjectCost(this.queryParams).then(response => {
        this.projectCostList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectCostId: undefined,
        projectCostType: undefined,
        projectCostCategory: undefined,
        projectCostFromUser: undefined,
        projectCostDate: undefined,
        projectCostAmount: undefined,
        projectCostToUser: undefined,
        projectCostContent: undefined,
        projectId: undefined,
        workOrderId: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeProjectCostDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectCostId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目费用";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const projectCostId = row.projectCostId || this.ids
      getProjectCost(projectCostId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改项目费用";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.projectCostId != null) {
            updateProjectCost(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addProjectCost(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const projectCostIds = row.projectCostId || this.ids;
      this.$modal.confirm('是否确认删除项目费用编号为"' + projectCostIds + '"的数据项？').then(() => {
        this.loading = true;
        return delProjectCost(projectCostIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/projectCost/export', {
        ...this.queryParams
      }, `projectCost_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
